﻿#region Using directives

using System;

#endregion

namespace NetROS.Tiers.Entities
{	
	///<summary>
	/// An object representation of the 'tblRazdobljeStopa' table. [No description found the database]	
	///</summary>
	/// <remarks>
	/// This file is generated once and will never be overwritten.
	/// </remarks>	
	[Serializable]
	[CLSCompliant(true)]
	public partial class RazdobljeStopa : RazdobljeStopaBase
	{		
		#region Constructors

		///<summary>
		/// Creates a new <see cref="RazdobljeStopa"/> instance.
		///</summary>
		public RazdobljeStopa():base(){}	
		
          protected override void AddValidationRules()
          {
              base.AddValidationRules();
              ValidationRules.AddRule(ValidateDatumOdDo,
                                      new NetROS.Tiers.Entities.Validation.ValidationRuleArgs("DatumOd", "Datum od"));
              ValidationRules.AddRule(ValidateDatumOdzaSifruNijeUIntervalima,
                                      new NetROS.Tiers.Entities.Validation.ValidationRuleArgs("DatumOd", "Datum od"));
          }

	    #endregion

         private bool ValidateDatumOdDo (object target, NetROS.Tiers.Entities.Validation.ValidationRuleArgs e)
    {
        if (this != null)
        {
            if ( this.DatumOd < this.DatumDo && DatumOd.Year == DatumDo.Year)
            {
                return true;
            }
            else
            {

                e.Description = string.Format("Datum od je veći ili jednak datumu do ili nije u istoj godini");
                return false;
            }
          
        }
        return true;
        
    }

        private bool ValidateDatumOdzaSifruNijeUIntervalima(object target, NetROS.Tiers.Entities.Validation.ValidationRuleArgs e)
        {
            if (this != null && this.ParentCollection != null && this.ParentCollection as TList<RazdobljeStopa> != null)
            {
                TList<RazdobljeStopa> _parentcol =  this.ParentCollection as TList<RazdobljeStopa>;

                DateTime _max = new DateTime(1790, 1, 1); 
                 DateTime _datumDoThis = new DateTime(9999,1,1);
                if (this.DatumDo != null)
                        {
                            _datumDoThis  = this.DatumDo;
                        }
                foreach (RazdobljeStopa _razdobljeStopa in _parentcol)
                {
                    if ( _razdobljeStopa != this)
                    {
                        //if (_konto.DatumDo != null  && _max < _konto.DatumDo) _max = _konto.DatumDo.Value;
                        DateTime _datumDo = new DateTime(9999,1,1);
                       
                       
                            _datumDo  =_razdobljeStopa.DatumDo;
                      

                        bool result = false;

                        if (this.DatumOd < _razdobljeStopa.DatumOd && this.DatumOd < _datumDo)
                        {
                            result = true;
                        }
                        else
                        {
                            result = false;
                        }
                        if (_datumDoThis < _razdobljeStopa.DatumOd && _datumDoThis < _datumDo)
                        {
                            result = true && result;
                        }
                        else
                        {
                            result = false;
                        }
                        bool result2 = false;
                        if (this.DatumOd > _razdobljeStopa.DatumOd && this.DatumOd > _datumDo)
                        {
                            result2 = true;
                        }
                        else
                        {
                            result2 = false;
                        }
                        if (_datumDoThis > _razdobljeStopa.DatumOd && _datumDoThis > _datumDo)
                        {
                            result2 = true && result2;
                        }
                        else
                        {
                            result2 = false;
                        }

                        if (!(result ^ result2))
                        {
                            e.Description = string.Format("Intervali se preklapaju");
                            return false;
                        }
                        

                        
                    }
                    //&& (this.DatumDo == null || this.DatumDo.Value <= _max)
                
                }
                //if (_max >= this.DatumOd)
                //{
                //    if (this.DatumDo == null)
                //    {
                //        return false;
                //    }
                //    if (this.DatumDo != null && this.DatumDo.Value <= _max)
                //    {
                //        return true;
                //    }

                //}
            }
            return true;

        }
	}
}
